package com.alibaba.datax.core.util;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;

public class AESUtils {

    private static final String AES_KEY = "AES_KEY";


    private static String keyStr = "ad1725339b2dd0a68903c57b6uyt23ca";

    public static String encrypt(String content, String key) {
        if (StrUtil.isBlank(content)) {
            return content;
        }
        byte[] KEY_BYTES = getKeyBytes(key);
        return HexUtil.encodeHexStr(SecureUtil.aes(KEY_BYTES).encrypt(content), false);
    }

    private static byte[] getKeyBytes(String key) {
        if (StrUtil.isBlank(key)) {
            key = keyStr;
        }
        byte[] KEY_BYTES = new byte[16];
        int i = 0;
        for (byte b : key.getBytes()) {
            KEY_BYTES[i++ % 16] ^= b;
        }
        return KEY_BYTES;
    }

    public static String decrypt(String content) {
        if (StrUtil.isBlank(content)) {
            return content;
        }
        return SecureUtil.aes(getKeyBytes(null)).decryptStr(content);
    }

    public static void main(String[] args) {
        System.out.println(decrypt("37C612CC32BE40154FA19886CE56765D"));
        System.out.println(decrypt(decrypt("979AD63F61607260F4ABBAC41C211ADA0240070159598AEF532436DEF406560982172EE0194424B544A75E3555B0FA14")));
    }

}